# Install and Setup OpenBSD

Tested with OpenBSD 6.9.

## Install

[Download](https://www.openbsd.org/faq/faq4.html#Download) `install69.img` for USB flash drive and `install69.iso` for CD/DVD. Check to see if checksum matches with `SHA512` file.

```sh
# From OpenBSD:
sha256 -C SHA256 install*.{img,iso}
# From GNU+Linux:
sha256sum -c --ignore-missing SHA256
```

### Write image:

**For USB:**

```sh
# From OpenBSD:
dd if=install*.img of=/dev/rsdXc bs=1m; sync
# From GNU+Linux:
dd if=install69.img of=/dev/sdX bs=1M status=progress; sync
```

**For CD/DVD:**

```sh
# From OpenBSD:
doas cdio tao install*.iso
```

Refer to [OpenBSD download page](https://www.openbsd.org/faq/faq4.html#MkInsMedia) for instructions.

### Install process:

The installer is mainly question based. After booting the medium, enter i to start the installer.

Keyboard layout: I enter "us". Enter ? for list and choose option for your keyboard.

**Host and network:**

- Hostname: "bsd" or anything you wish
- A bunch of network related questions: If you're on ethernet, just press enter to choose the defaults. If on wifi, read and the questions and answer as asked.
- DNS domain name? "localhost" is ok for desktop use.

**Basic setup questions:**

- Password for root account, confirm: enter a secure password
- Start sshd: choose yes if you want to access this machine remotely from another machine, otherwise no
- Do you expect to run X Win system? enter yes
- Do you want X to started with xenodm? enter yes if you wish to have a GUI login screen

**User setup:**

- Setup a user? enter username in lowercase letters, no space
- Full name? Enter a friendly name with uppercase, lowercase and spaces
- Enter password and confirm.
- What timezone? press ? for help, or enter if you know already

**Partitioning:**

- Available disks... will show you the disks that are present on the system currently. Remember the device id for install medium, we'll need it later.
- Which disk is the root disk? enter ? details, then enter the device id for installation
- Use (w)hole disk MBR, whole disk (G)PT.... or (E)? enter w to use the whole disk
- Use (A)uto layout, (E)dit, (C)ustom layout? enter c for custom layout (auto creates a whole bunch of partitions which may be messy to manage)
  - Enter ? for help, d [part] to delete part, `d *` to delete all part, a [part] to add a part, `p m` to see current layout
  - Ignore the partition named `c`, it will always stay there, you can't change it.
  - To create a basic system, enter `a a`. the first partition should always be mounted as /, otherwise openbsd bootloader will not be able to boot. When done, enter q to save all changes
- Write new label? Press enter
- Which disk do you want to init? Press enter for done

**Install Set names:**

*To install from the medium itself (offline):*

- Location of sets? enter disk
- Is the disk partition already mounted? enter n
- Which disk contains the upgrade media? enter the id for install medium, e.g. sd2
- Which sdX partition has the upgrade sets? enter to select a
- Pathname to the sets... press enter
- Set names?... press enter if not sure
- Continue without verification? enter y

*To install from internet:*

- Location of sets? enter http
- HTTP proxy? enter for none, or enter as you need
- HTTP Server? enter a host of a mirror without "http://" or "https://" and sub dirs. We will be given option to enter subdirs later. e.g. `ftp.jaist.ac.jp`, `mirrors.dotsrc.org` or enter ? or choose from [https://www.openbsd.org/ftp.html](https://www.openbsd.org/ftp.html)
- Server directory? default is fine, so you can press enter

- Select sets by entering... If you don't now what you're doing, you can press enter. (~1.7G install)

*To customize file sets:*

This is not generally needed, but if a lightweight system is aimed for, enter `-all` to deselect all, then enter the name (e.g. base66.tgz) or pattern (e.g. base*) to select items. More info about the file sets are here: https://www.openbsd.org/faq/faq4.html#FilesNeeded

It may ask again to select set names. Just press enter to finish the install.

If you mess up and want to install any missing file sets: <https://www.cyberciti.biz/faq/openbsd-install-sets-after-install/>

Then you will get option to reboot. Reboot and enjoy!

Further reading:<br/>
<https://www.gabsoftware.com/tutorials/tutorial-how-to-install-openbsd-6-1-step-by-step/><br/>
<https://linoxide.com/how-tos/install-openbsd-easiest-guide-ever/>

## `doas`

Do this to run commands as root. Login as root and enter:

`vi /etc/doas.conf`
```
permit persist :wheel
```

`persist` is so that it doesn't ask for password again for a while.

## "Reordering libraries" step takes way too long at boot time

This is a security feature. But if you insist on disabling it:

```sh
doas rcctl disable library_aslr
```

Source: <https://libredd.it/r/openbsd/comments/8vc9mh/reordering_libraries_at_boot_incredibly_slow/e1n5bd1/?context=3>

## How to connect to USB Ethernet from phone

```sh
ifconfig # to find out tethered interface name, usually urndis0
doas dhclient urndis0
# Auto connect on boot:
echo dhcp | doas tee /etc/hostname.urndis0
```

## To set mirror

Mirrors can be found [here](https://www.openbsd.org/ftp.html).

**Temporary way:**

```sh
export PKG_PATH=ftp://your.ftp.mirror/pub/OpenBSD/`uname -r`/packages/`machine -a`/
```

e.g.

```sh
export PKG_PATH=https://ftp.jaist.ac.jp/pub/OpenBSD/`uname -r`/packages/`machine -a`/
export PKG_PATH=https://mirrors.dotsrc.org/pub/OpenBSD/`uname -r`/packages/`machine -a`/
```

**Permanent way:**

`doas vi /etc/installurl`
```
#https://cdn.openbsd.org/pub/OpenBSD
https://ftp.jaist.ac.jp/pub/OpenBSD
```

## Patching system

Run this after installing:

```sh
su
syspatch
pkg_add -Uu
sysmerge -d
```

## `Premature end of archive in header` when installing or upgrading packages

[Changing the mirror URL](https://www.mail-archive.com/misc@openbsd.org/msg177432.html) (as described above) should solve the issue.

If that doesn't work, you can try something like `doas sh -c 'until pkg_add -uU; do sync; done'` until there are no more packages to be updated.

## To install missing drivers + firmware

```sh
fw_update
```

Source:<br/>
<https://www.openbsdhandbook.com/installation/><br/>
<https://sohcahtoa.org.uk/openbsd.html>

## Upgrading all packages

```sh
doas pkg_add -u
```

## Package management

```sh
# install
doas pkg_add package
# remove
doas pkg_delete package
# search
pkg_info -c -Q package
```

```sh
# reinstall
doas pkg_add -D installed -r packagenamehere
# ignore ambiguities and install the extact package name given
doas pkg_add packagenamehere--
# for example:
doas pkg_add openvpn--
# remove orphan packages (installed to satisfy dependencies but which are no longer needed)
doas pkg_delete -a
```

Further reading: <https://cromwell-intl.com/open-source/package-management.html>

## Handling services

Example with `xenodm` service:

```sh
doas rcctl enable xenodm
doas rcctl disable xenodm
doas rcctl start xenodm
doas rcctl stop xenodm
doas rcctl restart xenodm
```

## Starting a GUI DE/WM

`touch .xession && chmod +x .xsession`<br/>
`vi ~/.xsession`
```sh
# Start DBUS session bus:
if [ -x "$(command -v dbus-launch)" && -z $DBUS_SESSION_BUS_ADDRESS ]; then
  eval `dbus-launch --sh-syntax --exit-with-x11`
fi
exec openbox-session
```

Then login from xenodm screen as user.

The `exec` line above is for Openbox. Adjust for your DE/WM.

## Disable `.core` file creation

`~/.profile`
```
...
# on tty
ulimit -c 0
```

`~/.xsession`
```
...
# on xorg sessions
ulimit -c 0
```

Source: <http://daemonforums.org/showthread.php?t=7149>

## Run shell commands on startup

`touch ~/.kshrc && chmod +x ~/.kshrc`
`vi ~/.xsession`
```
[ -f $HOME/.kshrc ] && export ENV=$HOME/.kshrc
...
```

\* Add the above code before exec session commands. Better to put it above every other command. Otherwise it will not work.

`vi ~/.profile`
```
[ -f $HOME/.kshrc ] && export ENV=$HOME/.kshrc
...
```

Example `~/.kshrc`
```
# change shell prompt
PS1='\w \$ '
```

## Remembering command history between sessions

By default, ksh doesn't store history between sessions. To store history:

`vi ~/.kshrc`
```
HISTFILE="$HOME/.ksh_history"
HISTSIZE=3000
```

List of previous commands will be stored in the file mentioned in `HISTFILE`. Number of commands to store is indicated by `HISTSIZE`. Adjust accordingly. Previous commands will be available with Ctrl+R search, up arrow or any other way to access history.

Since the file stores your last run commands, it is a better idea to do this on multi user system so that no other user can read this:

```sh
chmod 600 ~/.ksh_history
```

## Using alises

Bash aliases are more or less compatible with ksh without edits. So any `.bash_aliases` may be used with it (such as `bash/.bash_aliases` in this repo).

`~/.kshrc`
```
[ -f $HOME/.bash_aliases ] && . $HOME/.bash_aliases
```

`. ~/.kshrc # to update changes for current shell`

## Map ThinkPad back-next button to home-end

This is a way to map the back and next button (positioned left and right to the up arrow button) to home and end keys.

`~/.xsession`
```
# map thinkpad back-next button to home-end button
xmodmap -e 'keycode 234 = Home'
xmodmap -e 'keycode 233 = End'
...
```

## Enable ThinkPad Trackpoint scrolling

`~/.xsession`

```
# enable scrolling with a combination of trackpoint and middle button
# remember to put this before exec session command
xinput set-prop "/dev/wsmouse" "WS Pointer Wheel Emulation" 1
xinput set-prop "/dev/wsmouse" "WS Pointer Wheel Emulation Button" 2
xinput set-prop "/dev/wsmouse" "WS Pointer Wheel Emulation Axes" 6 7 4 5
...
```

## How to disable Emacs keybindings

By default, ctrl+a takes cursor to beginning, instead of select all and ctrl+/ selects all text. Emacs keyboard shortcut theme is set by default.

To fix:

```sh
gsettings set org.gnome.desktop.interface gtk-key-theme "Default"
```

If it doesn't work:

`vi ~/.config/gtk-3.0/settings.ini`
```
[Settings]
...
gtk-key-theme-name = Default
```

If it doesn't work for GTK 2 apps:

`vi ~/.gtkrc-2.0`
```
...
gtk-key-theme-name = "Default"
```

Ref:<br/>
<https://askubuntu.com/questions/294953/gtk-key-theme-emacs-what-keystroke-is-for-select-all><br/>
<https://askubuntu.com/questions/124815/how-do-i-enable-emacs-keybindings-in-apps-such-as-google-chrome>

## How to automount USB devices

```sh
doas -s
pkg_add hotplug-diskmount
/usr/local/libexec/hotplug-diskmount init
echo hotplugd_flags= >> /etc/rc.conf.local
cp /usr/local/share/doc/pkg-readmes/hotplug-diskmount /etc/hotplug/attach
chmod +x /etc/hotplug/attach
```

`vi /etc/hotplug/attach`

Now delete everything keeping only lines "#!/bin/sh" to "esac". Change "joeuser" in the LOGIN line to your normal username.

```sh
rcctl start hotplugd
exit
```

Plug your USB storage devices and it should show up in `/vol/`.
If it doesn't work, reboot.

**Tip**: You can right click /vol directory on your file manager and bookmark it or send to > sidepane or anything equivalent to get easier access to mounted drives.

Ref:<br/>
<https://libredd.it/r/openbsd/comments/fvmy29/looking_to_set_up_automount_for_usb_sticks/fmmsj0m/?context=3><br/>
<https://www.tumfatig.net/20110903/automount-usb-stick-on-openbsd/><br/>
<https://unix.stackexchange.com/a/417571>

## Man pages missing?

[Install](https://www.cyberciti.biz/faq/openbsd-install-sets-after-install/) `manXX.tgz` from file sets.

## No sound

```sh
sndioctl # lists all options
sndioctl output.mute=0 # to unmute main output
# if still having problems:
sndioctl output.level=1
```

You can also do `doas pkg_add cmixer` and solve the issue by running `cmixer` then adjusting manually.

Details: <https://www.openbsd.org/faq/faq13.html>

## Install TOR Browser

```sh
doas pkg_add tor-browser
```

## Firefox troubleshooting

### Firefox video playback issue

On Firefox, if there is an error message showing saying "No compatible source was found for this media." in the video area, this may fix it:

```sh
doas pkg_add ffmpeg gstreamer1-plugins-libav
```

### Firefox is slow

Below are suggested around the internet and pkg-readmes. Depending on your setup, some of them might (or might not) work.

- Try running a compositor such as `xcompmgr`
- Set `layers.acceleration.force-enable` to `true` on about:config
- If still doesn't work, set `gfx.webrender.enabled` to true on about:config
- `sysctl hw.smt=1` (it is a security risk though)
- `usermod -G wheel,staff yourusername`
- add `,softdeps` or `,noatime` after `rw` on your /etc/fstab (you may have to experiment which one works best on your system)
- Use a lightweight window manager (such as openbox, jwm etc.)
- Configure your video driver for TearFree (intel) or glamor (radeon)
- Use a ramdisk for disk caching in Iridium or Firefox (man mount_mfs)
- Increasing maxfiles: `doas sysctl kern.maxfiles=16000; echo "kern.maxfiles=16000" | doas tee -a /etc/sysctl.conf`

Ref:
- Check `/usr/local/share/doc/pkg-readmes/firefox` for more information.
- <https://old.reddit.com/r/openbsd/comments/9iyqfb/firefox_unbearably_slow/e6oi3fq/>
- <https://old.reddit.com/r/openbsd/comments/q0il9q/slow_gui/>
- <https://old.reddit.com/r/openbsd/comments/ljwfe3/is_openbsd_actually_slow_or_inefficient/>

## Upgrade from OpenBSD -stable or -release to -current

[ Moved to [`openbsd-current.md`](openbsd-current.md) ]

## Install and use OpenBSD ports

OpenBSD ports are sources for the packages available. Not to be confused with port accompanied by an IP address.

**Setting up:**

```sh
$ cd /tmp
$ ftp https://cdn.openbsd.org/pub/OpenBSD/$(uname -r)/{ports.tar.gz,SHA256.sig}
$ signify -Cp /etc/signify/openbsd-$(uname -r | cut -c 1,3)-base.pub -x SHA256.sig ports.tar.gz
```

```
# cd /usr
# tar xvzf /tmp/ports.tar.gz
# rm /tmp/ports.tar.gz
```

**Searching:**

`rsnapshot` is shown as an example port in the commands below. Use your port name instead.

```sh
$ cd /usr/ports
$ doas pkg_add portslist
$ make search key=rsnapshot # change rsnapshot with query
```

**To run builds as normal user:**

`doas nano /etc/mk.conf`
```
WRKOBJDIR=/usr/obj/ports
DISTDIR=/usr/distfiles
PACKAGE_REPOSITORY=/usr/packages
```

```sh
$ doas mkdir -p /usr/{obj/ports,distfiles,packages}
$ doas chown -R $USER:$USER /usr/{obj,distfiles,packages} # whole /usr/obj!!
```

**Installing a port:**

```sh
$ cd /usr/ports/net/rsnapshot
$ make install # if it doesn't work, run with doas
```

**Cleaning build leftovers:**

```sh
$ cd /usr/ports/net/rsnapshot
$ make clean
$ doas pkg_delete -a
$ make clean=dist
$ make clean=flavors
```

**Uninstalling a port:**

```sh
$ cd /usr/ports/net/rsnapshot
$ make uninstall
$ make clean=packages
```

Ref:<br/>
<https://www.openbsd.org/faq/ports/ports.html><br/>
<https://www.cyberciti.biz/faq/openbsd-install-ports-collection/>

## Further reading

- <https://www.romanzolotarev.com/openbsd/install.html>
- <https://www.k58.uk/openbsd.html>
- <https://sohcahtoa.org.uk/openbsd.html>
- <http://kestrel.nmt.edu/~raymond/software/howtos/openbsd.html>
